/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;



import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import transervfx.Dato.Entidad.Agencia;
import transervfx.Dato.Entidad.Externa;

/**
 *
 * @author oscar
 */

public class AgenciaDAO implements Dao<Agencia,Long> {
 
     private static AgenciaDAO INSTANCE = null;
    
    
     /**
     *  Crea el Gestor Auxiliar
     */
    protected AgenciaDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas  multi-hilo
     * otra prueba para evitar instanciación múltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new AgenciaDAO();
        }
    }

    /**
     * Obtiene la instancia.
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static AgenciaDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }
    

    @Override
    public void persist(Agencia entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
              
                em.persist(entity);
               
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
     //Sin transaccion
    public void remove(Agencia entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.remove(entity);

        } finally {
            em.close();
        }
    }
   
    @Override
    public void update(Agencia entity) {
          EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

          em.getTransaction().begin();
          if(em.contains(entity))
          {
            em.merge(entity);
          }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
    }

    @Override
    public Agencia findById(Long id) {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(Agencia.class, id);

        } finally {
            em.close();
        }
    }
    
    
    public Agencia findByNombre(String nombre) {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
       EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createNamedQuery("Agencia.findByNombre");
            query.setParameter("nombre", nombre);
            List<Agencia> lu =  query.getResultList();
            if(lu.size()>0){
                return lu.get(0);
            }else{
                return null;
            }

        } finally {
            em.close();
        }
    }
    

  
    @Override
    public List<Agencia> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Agencia.findAll").getResultList();

        } finally {
            em.close();
        }
    }
    
  
    public List<Agencia> findAll(int ini,int fin) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
        Query q=em.createNamedQuery("Agencia.findAll");
       
        q.setFirstResult(ini);
        q.setMaxResults(fin);
            return q.getResultList(); 

        } finally {
            em.close();
        }
    }
    
  

    public Long numeroDeAgencias() {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return (Long) em.createNamedQuery("Agencia.count").getSingleResult();

        } finally {
            em.close();
        }
    }

    public List<Externa> findAllExternas() {
         EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
        Query q=em.createNamedQuery("Externa.findAll");
        List<Externa> listE=q.getResultList();
        if(!listE.isEmpty()){
            listE.get(0).getDomicilio();
        }
            return q.getResultList(); 

        } finally {
            em.close();
        }
    }

 
    
  
}
